/*
 * @Author: yangrongxin
 * @Date: 2021-08-05 10:44:37
 * @LastEditors: guoju
 * @LastEditTime: 2022-01-13 17:53:39
 */
import { startexam } from '@/services/exam';
import { caclTime } from '@/utils/common';
import { CHANNEL_TYPE } from '@/pages/My/compoents/MyAssessment/dataDictionary';
import { examData } from '../EvaluationModule/pages/CheckboxAnwser/_mock';
import type { StartResult } from '../types/exam';

const Exam: any = {
  namespace: 'evaluation',
  state: {
    examData: {
      duration: 0, // 考试时间
      examRecordId: '', // 考试记录id
      questions: [], // 当前所有题目列表
      questionNum: 0, // 考题数量
      memberView: 1, // 判断当前用户能不能查看报告 1 是可以 0 是不行
    },
    // 记录当前是第几道题
    curQuestionIndex: 0,
    // 存储答案数据
    submitQuestionInfoList: [],
    // 存储测评所需的密码
    examPassword: '',
    /* 记录跳题序号
     * {
          questionId, // 当前跳题选项的单选题id
          curIndex: curQuestionIndex, // 当前跳题选项的单选题序号
          jumpIndex: index // 要跳题的序号
        }
     */
    jumpQuestions: [],
  },
  effects: {
    // 获取当前测评的数据
    *startexam({ payload }, { call, put, select }) {
      const password = yield select((state: any) => state.evaluation.examPassword);
      const { evaluationType } = payload;

      const examData: StartResult = yield call(startexam, {
        ...payload,
        password,
      }) || {};

      const { examRecordId, questionNum, questions } = examData;

      // 当前是院内的时候 院内的数据记录 需要增加对应的选择时间
      if ( evaluationType === CHANNEL_TYPE.THECOURT ) {
        questions.forEach((item) => {
          Object.assign(item, {
            ...caclTime(item)
          });
        });
      }
      yield put({
        type: 'updateExamData',
        payload: {
          curQuestionIndex: 0,
          submitQuestionInfoList: [],
          examData: {
            examRecordId,
            questionNum,
            questions
          }
        },
      });
    },
  },
  reducers: {
    // 更新当前全局的试题数据
    updateExamData(state, { payload }) {
      const { 
        examData: stateExamData,
        curQuestionIndex: stateCurQuestionIndex,
        submitQuestionInfoList: stateSubmitQuestionInfoList
      } = state;
      const {
        jumpQuestions = [],
        examData: payloadExamData,
        curQuestionIndex: payloadCurQuestionIndex,
        submitQuestionInfoList: payloadSubmitQuestionInfoList,
      } = payload;

      const examData = payloadExamData || stateExamData;
      const curQuestionIndex = !isNaN(payloadCurQuestionIndex) ? payloadCurQuestionIndex : payloadCurQuestionIndex || stateCurQuestionIndex;
      const submitQuestionInfoList = payloadSubmitQuestionInfoList || stateSubmitQuestionInfoList;
      
      return {
        ...state,
        jumpQuestions,
        curQuestionIndex,
        submitQuestionInfoList,
        examData: {
          ...examData
        },
      };
    },
    // 提交一个答案
    addAnswer(state, { payload }) {
      let { curQuestionIndex } = state;
      const { submitQuestionInfoList, jumpQuestions } = state;
      const { answer, isLast, callback } = payload;
      const {
        examData: { examRecordId },
      } = state;

      const newSubmitQuestionInfoList = JSON.parse(JSON.stringify(submitQuestionInfoList));
      newSubmitQuestionInfoList[curQuestionIndex] = answer;

      if (!isLast) {
        // 下一题 跳题处理
        const isJump =
          jumpQuestions.length > 0 &&
          jumpQuestions.find(
            (items: any) => items.curIndex === curQuestionIndex,
          );

        if (isJump) {
          for (let i = isJump.curIndex + 1; i < isJump.jumpIndex; i += 1) {
            newSubmitQuestionInfoList[i] = null;
          }
          curQuestionIndex = isJump.jumpIndex;
        } else curQuestionIndex += 1;
      }

      if (isLast) {
        callback &&
          callback({
            examRecordId,
            submitData: newSubmitQuestionInfoList,
            appVersion: VERSION,
          });
      }
      return {
        ...state,
        submitQuestionInfoList: newSubmitQuestionInfoList,
        curQuestionIndex,
      };
    },
    // 返回上一题
    prevAnswer(state, { payload }) {
      const { curQuestionIndex, submitQuestionInfoList, jumpQuestions } = state;

      // 获取上一道题的索引 跳题处理
      let prevIndex: number = curQuestionIndex;
      const isJump =
        jumpQuestions.length > 0 &&
        jumpQuestions.find(
          (items: any) => items.jumpIndex === curQuestionIndex,
        );
      if (isJump) prevIndex = isJump.curIndex;
      else prevIndex -= 1;

      // 保存当前题目的选择
      const newSubmitQuestionInfoList = JSON.parse(JSON.stringify(submitQuestionInfoList));
      newSubmitQuestionInfoList[curQuestionIndex] = payload.answer;

      // 当前题目不存在
      if (prevIndex < 0) {
        // 如果是不存在的上一题 返回空
        payload.callback &&
          payload.callback({
            error: true,
          });
        return {
          ...state,
        };
      }
      return {
        ...state,
        submitQuestionInfoList: newSubmitQuestionInfoList,
        curQuestionIndex: prevIndex,
      };
    },
    // 更新当前的测评密码
    updatePassword(state, { payload }) {
      return {
        ...state,
        examPassword: payload.password,
      };
    },
  },
};

export default Exam;
